import type { DataTypes, ExtendInfoBO, GetOrgTreeParams, GetUserListParams, GetUserParams, ResponseBo, TransferUsers, UserBO, UserInfoBO } from '@/model/UserBo';
import http from '@/utils/RequestUtils';

// 统一前缀
const commonUrlPrefix = `${import.meta.env.VITE_API_BASE_FOUNDATION}/api/v1`;

/**
 * 获取用户管理左侧组织树数据
 * @param data  {"orgCode": "","isTree": "1","loadType": "","extendCondition":""}
 * @returns 组织树数据
 */
export function getOrgTreeOriginalData(data: GetOrgTreeParams): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/extend-org/load`, data);
}

/**
 * 获取用户列表
 * @param data {"orgCode": ""}
 * @returns 用户列表数据
 */
export function getUserList(data: GetUserListParams): Promise<ResponseBo<UserInfoBO>> {
  return http.post(`${commonUrlPrefix}/user-manager/user/list`, data);
}

/**
 * 获取用户所属组织信息
 * @param data {"userIds": "", "viewType": "withOrgList"}
 * @returns 用户组织列表数据
 */
export function getUserOrgList(data: GetUserParams): Promise<ResponseBo<UserBO[]>> {
  return http.get(`${commonUrlPrefix}/user-manager/user/org-list`, data);
}

/**
 * 加载扩展信息
 * @param data {"userId": "", "orgCode": ""}
 * @returns 用户扩展信息数据
 */
export function getUserExtraInfo(data: GetUserParams): Promise<ResponseBo<ExtendInfoBO>> {
  return http.get(`${commonUrlPrefix}/user-manager/user/org/extra-info`, data);
}

/**
 * 更新扩展信息
 * @param data {"userId": "", "orgCode": "","extraInfo": ""}
 * @returns 更新结果
 */
export function updateUserExtraInfo(data: GetUserParams): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/org/extra-info/update`, data);
}

/**
 * 用户状态变更
 * @param data {items: [], "status": ""}
 * @returns 状态变更结果
 */
export function changUserStatus(data: TransferUsers): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/org/status`, data);
}

/**
 * 全部用户状态变更
 * @param data {"userIds": "", "status": ""}
 * @returns 状态变更结果
 */
export function changAllUserStatus(data: GetUserParams): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/orgs/status`, data);
}

/**
 * 用户转入
 * @param data
 * {
    "keepOriginal": 1,
    "targetOrgCode": "ORG_X",
    "items": [
      { "usersCode": "U001,U002", "sourceOrgCode": "ORG_A" },
      { "usersCode": "U003",      "sourceOrgCode": "ORG_B" }
    ]
  }
 * @returns 转入结果
 */
export function transferUsersIn(data: TransferUsers): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/org/transfer-in`, data);
}

/**
 * 用户转出
 * @param data
 * {
  "targetOrgCode": "ORG_X",
  "keepOriginal": 1,
  "items": [
    { "usersCode": "U001,U002", "sourceOrgCode": "ORG_A" },
    { "usersCode": "U003", "sourceOrgCode": "ORG_B" }
  ]
}
 * @returns 转出结果
 */
export function transferUsersOut(data: TransferUsers): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/org/transfer-out`, data);
}

/**
 * 从本组织中将用户移除
 * @param data {"userIds": "","orgCode": ""}
 * @returns 移出结果
 */
export function removeUser(data: GetUserParams[]): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/org/remove`, data);
}

/**
 * 下线
 * @param data {"userId": "","orgCode": ""}
 * @returns 下线结果
 */
export function offlineUser(data: TransferUsers): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/offline`, data);
}

/**
 * 获取组织用户树
 * @param data {"orgCode": "","isTree": "1","loadType": "","extendCondition":""}
 * @returns 组织用户树数据
 */
export function getOrgTreeUserData(data: GetOrgTreeParams): Promise<ResponseBo<DataTypes[]>> {
  return http.post(`${commonUrlPrefix}/extend-org/load-org-user`, data);
}

/**
 * 获取组织用户树
 * @param data {"orgCode": "","loadMode": "", "loadType": "", "keyword": ""}
 * @returns 组织用户树数据
 */
export function queryOrgTreeUserData(data: GetOrgTreeParams): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/extend-org/load-org-user/query`, data);
}

/**
 * 重置密码
 * @param data {"userId": ""}
 * @returns 重置结果
 */
export function resetPassword(data: GetUserParams): Promise<ResponseBo> {
  return http.post(`${commonUrlPrefix}/user-manager/user/reset-password`, data);
}
